package io.gitee.thinkbungee.utils;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.stereotype.Component;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * spring工具类
 *
 * @author chenyichang
 * @since 2022/5/31
 */
@Component
public class ApplicationContextUtils implements ApplicationContextAware {

    /**
     * 全局上下文context
     */
    public static ApplicationContext applicationContext;

    /**
     * 异步发布器 bean name
     */
    private static final String SATELLITE_ASYNC_APPLICATION_EVENT_MULTICASTER_BEAN_NAME =
            "satelliteAsyncApplicationEventMulticaster";

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ApplicationContextUtils.applicationContext = applicationContext;

        //注册自己的异步事件发布器
        if (applicationContext instanceof AbstractApplicationContext) {
            AbstractApplicationContext abstractApplicationContext = (AbstractApplicationContext) applicationContext;
            SimpleApplicationEventMulticaster simpleApplicationEventMulticaster =
                    new SimpleApplicationEventMulticaster(abstractApplicationContext.getBeanFactory());
            abstractApplicationContext.getBeanFactory().registerSingleton(SATELLITE_ASYNC_APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
                    simpleApplicationEventMulticaster);
            //设置执行者
            simpleApplicationEventMulticaster.setTaskExecutor(new ThreadPoolExecutor(
                    Runtime.getRuntime().availableProcessors(),
                    Runtime.getRuntime().availableProcessors(),
                    10,
                    TimeUnit.MINUTES,
                    new ArrayBlockingQueue<>(16 * Runtime.getRuntime().availableProcessors(), true)
            ));
        }
    }

    /**
     * 根据名称和类型获取bean
     *
     * @param name         名称
     * @param requiredType 类型
     * @param <T>          泛型
     */
    public static <T> T getBean(String name, Class<T> requiredType) {
        return applicationContext.getBean(name, requiredType);
    }

    /**
     * 根据名称和类型获取bean
     *
     * @param requiredType 类型
     * @param <T>          泛型
     */
    public static <T> T getBean(Class<T> requiredType) {
        return applicationContext.getBean(requiredType);
    }

    /**
     * 是否存在该bean
     *
     * @param beanName bean
     * @return 是否存在
     */
    public static Boolean containsBean(String beanName) {
        if (StrUtil.isBlank(beanName)) {
            return false;
        }
        return applicationContext.containsBean(beanName);
    }

    /**
     * 发布事件
     *
     * @param applicationEvent 事件
     */
    public static void publishEvent(ApplicationEvent applicationEvent) {
        SimpleApplicationEventMulticaster eventMulticaster =
                applicationContext.getBean(SATELLITE_ASYNC_APPLICATION_EVENT_MULTICASTER_BEAN_NAME,
                        SimpleApplicationEventMulticaster.class);
        eventMulticaster.multicastEvent(applicationEvent);
    }

    public static void publishSyncEvent(ApplicationEvent applicationEvent) {
        applicationContext.publishEvent(applicationEvent);
    }
}
